<?php
namespace app\admin\controller;
use app\logic\DataHelper;
use app\logic\GoodsHelper;
use app\logic\MenuHelper;
use think\facade\Db;
use think\facade\View;
class Goods extends Base
{
    public function index() {
        return View::fetch('index');
    }

    public function load_category() {
        $tree = GoodsHelper::load_goods_category(0); // MenuHelper::load_menu(0);

        return json([
            'code' => 0,
            'msg' => '成功',
            'tree' => $tree
        ]);
    }

    public function save() {
        $params = get_all_params();

        if (!check_params_empty(['goods_no'])) {
            return json([ 'code' => -1, 'msg' => '请填写：goods_no' ]);
        }

        $goods_id = 0;
        if (isset($params["id"]) && $params["id"] > 0) {
            $goods_id = $params["id"];
            DataHelper::update("tb_goods", "id=".$params["id"], $params);
        } else {
            $goods_id = DataHelper::add("tb_goods", $params);
        }
        GoodsHelper::update_goods_spec($goods_id);

        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function edit($id = 0) {
        if ($id > 0) {
            $entry = DataHelper::find_by_id("tb_goods", $id);
            $entry['category_name'] = DataHelper::get_value('tb_goods_category', "id=".$entry['category'], 'title');
        } else {
            $entry = [
                'id' => '0',
                'goods_no' => '',
                'goods_name' => '',
                'cover' => '',
                'body' => '',
                'published' => '',
                'category' => '',
                'category_name' => '',
                'status' => '',

                'spec1_status' => '0',
                'spec1_label' => '',
                'spec1_value' => '',
                'spec2_status' => '0',
                'spec2_label' => '',
                'spec2_value' => '',
                'spec3_status' => '0',
                'spec3_label' => '',
                'spec3_value' => '',
            ];
        }
        return View::fetch('edit', [
            "entry" => $entry
        ]);
    }

    public function list($goods_no = "", $page = 1, $limit = 20) {
        $query = " status=0 ";
        if (!empty($goods_no)) {
            $query .= " and goods_no like '%$goods_no%' ";
        }

        list($list, $count) = DataHelper::select_page("tb_goods", $query);
        if ($count <= 0) {
            return json([ 'code' => 201, 'msg' => '没有相关数据' ]);
        }

        foreach ($list as &$item) {
            $item['category_name'] = DataHelper::get_value('tb_goods_category', "id=".$item['category'], 'title');
            $item['published'] = ($item['published'] == 1 ? '是' : '否');
        }

        return json([ 'code' => 0, 'msg' => '成功', 'count' => $count, 'data' => $list ]);
    }

    public function del() {
        $params = get_all_params();
        $ids = $params['ids'];
        DataHelper::update('tb_goods', "id in ($ids)", [
            'status' => -1
        ]);
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function spec_list($goods_id) {

        return View::fetch('spec_list', [
            "goods_id" => $goods_id
        ]);
    }

    public function spec_list_list($goods_id = "", $page = 1, $limit = 100) {
        $query = " status=0 ";

        list($list, $count) = DataHelper::select_page("tb_goods_spec", $query, [ 'page' => $page, 'limit' => $limit ]);
        if ($count <= 0) {
            return json([ 'code' => 201, 'msg' => '没有相关数据' ]);
        }


        return json([ 'code' => 0, 'msg' => '成功', 'count' => $count, 'data' => $list ]);
    }

    public function del_spec() {
        $params = get_all_params();
        $ids = $params['ids'];
        DataHelper::update('tb_goods_spec', "id in ($ids)", [
            'status' => -1
        ]);
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function update_spec_value() {
        $params = get_all_params();
        $id = $params['id'];
        $field = $params['field'];
        $value = $params['value'];

        if (in_array($field, ['stock_num', 'price']) && (!is_numeric($value) || $value < 0)) {
            return json([ 'code' => -1, 'msg' => '数值错误' ]);
        }

        DataHelper::update('tb_goods_spec', "id in ($id)", [
            $field => $value
        ]);
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }
}
